package com.yinet.web.controller.touchscreen;

import com.yinet.common.config.Global;
import com.yinet.common.config.ServerConfig;
import com.yinet.common.core.controller.BaseController;
import com.yinet.common.core.page.TableDataInfo;
import com.yinet.common.utils.QRCodeUtils;
import com.yinet.common.utils.ServletUtils;
import com.yinet.system.domain.GuidanceFile;
import com.yinet.system.domain.TouchScreenGuidance;
import com.yinet.system.domain.TouchScreenNotice;
import com.yinet.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author ruoyi
 */
@Controller
@RequestMapping("/touchscreen")
public class BaseWebController extends BaseController {
    static String touchIndex = "touchscreen/web/";
    @Autowired
    private TouchScreenHelpInfoService touchScreenHelpInfoService;
    @Autowired
    private TouchScreenNoticeService touchScreenNoticeService;
    @Autowired
    private TouchScreenGuidanceService touchScreenGuidanceService;
    @Autowired
    private IGuidanceFileService guidanceFileService;
    @Autowired
    private BjcxService bjcxService;

    /**
     * 首页
     *
     * @return
     */
    @GetMapping(value = "/index")
    public String Index() {
        return touchIndex + "login";
    }

    /**
     * 中心介绍
     *
     * @return
     */
    @GetMapping(value = "/jieshao")
    public String GetInfo(Model model) {
        model.addAttribute("helpInfo", touchScreenHelpInfoService.selectHelpInfoLimit(1));
        return touchIndex + "jieshao";
    }

    /**
     * 办件查询
     *
     * @return
     */
    @GetMapping(value = "/search")
    public String Search() {
        return touchIndex + "search";
    }

    /**
     * 办事指南
     *
     * @return
     */
    @GetMapping(value = "/nan")
    public String GetHelp(Model model) {
        List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectParentGuidanceList();
        model.addAttribute("guidanceList", guidanceList);
        return touchIndex + "nan";
    }

    /**
     * 公示公告
     *
     * @return
     */
    @GetMapping(value = "/notice")
    public String GetNotice() {
        return touchIndex + "notice";
    }

    /**
     * 公示公告列表
     *
     * @return
     */
    @PostMapping("/notice/list")
    @ResponseBody
    public TableDataInfo GetNoticeList() {
        startPage();
        List<TouchScreenNotice> touchScreenNotices = touchScreenNoticeService.selectNoticeList();
        return getDataTable(touchScreenNotices);
    }

    /**
     * 公示公告详细信息
     *
     * @return
     */
    @GetMapping(value = "/notice/{id}")
    public String GetNoticeInfo(@PathVariable("id") String id, Model model) {
        TouchScreenNotice touchScreenNotice = touchScreenNoticeService.selectNoticeById(id);
        model.addAttribute("noticeinfo", touchScreenNotice);
        return touchIndex + "noticeinfo";
    }

    /**
     * 实时叫号
     *
     * @return
     */
    @GetMapping(value = "/hao")
    public String CallNum() {
        return touchIndex + "hao";
    }

    /**
     * 办件统计
     *
     * @return
     */
    @GetMapping(value = "/tong")
    public String 统计() {
        return touchIndex + "tong";
    }

    /**
     * 办事指南列表
     */
    @GetMapping(value = "/guidanceList/{id}")
    public String guidanceList(@PathVariable("id") String id, Model model) {
        List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectGuidanceListByParentId(id);
        model.addAttribute("guidanceList", guidanceList);
        return touchIndex + "list";
    }

    /**
     * 查看文档
     *
     * @return
     */
    @GetMapping(value = "/guidanceList/viewdoc/{id}")
    public String ViewDoc(@PathVariable("id") String id, Model model) throws IOException {
        GuidanceFile guidanceFile = guidanceFileService.selectGuidanceFileById(id);
        TouchScreenGuidance touchScreenGuidance = touchScreenGuidanceService.selectGuidanceById(guidanceFile.getGuidanceId());
        String domain = ServerConfig.getDomain(ServletUtils.getRequest());
        String qrUrl = domain + guidanceFile.getFileUrl();
        String s = QRCodeUtils.generalQRCode(qrUrl);
        guidanceFile.setQrCode(s);
        model.addAttribute("guidance", touchScreenGuidance);
        model.addAttribute("guidanceFile", guidanceFile);
        String url = Global.getKKFileView();
        model.addAttribute("kkview", url);
        return touchIndex + "viewdoc";
    }

    /**
     * 查看文档
     *
     * @return
     */
    @GetMapping(value = "/guidanceList/viewdoc/{type}/{id}")
    public String viewDocByType(@PathVariable("id") String id, @PathVariable("type") String type, Model model) throws IOException {
        GuidanceFile temp = new GuidanceFile();
        temp.setFileType(type);
        temp.setGuidanceId(id);
        List<GuidanceFile> guidanceFiles = guidanceFileService.selectGuidanceFileList(temp);
        if (guidanceFiles.size() == 1) {
            return ViewDoc(guidanceFiles.get(0).getFileId(), model);
        } else {
            model.addAttribute("guidanceFiles", guidanceFiles);
            return touchIndex + "list2";
        }
    }


    /**
     * 页头
     *
     * @return
     */
    @GetMapping(value = "/header")
    public String GetHeader() {
        return touchIndex + "tsheader";
    }

    /**
     * 页脚
     *
     * @return
     */
    @GetMapping(value = "/bottom")
    public String GetFooter() {
        return touchIndex + "bottom";
    }

    @PostMapping("/bjcx")
    @ResponseBody
    public String bjcx(String sbh, String password) {
        return bjcxService.bjcx(sbh, password);
    }
}
